//
//  GymRootViewController.m
//  YunDongAdmin
//
//  Created by lqq on 2018/9/6.
//  Copyright © 2018年 cloudfighting.com. All rights reserved.
//


typedef NS_ENUM(NSInteger, GymRootVCCellType) {
    GymRootVCCellType_GymGroup,//门店团队
    GymRootVCCellType_GymMember,//门店会员
    GymRootVCCellType_PlanPreview,//排课总览
    GymRootVCCellType_ClassAnalysis,//课时分析
    GymRootVCCellType_ArrivalGymAnalysis,//到店分析
    GymRootVCCellType_CoachAnalysis,//教练分析
};


#import "GymRootViewController.h"
#import "CommonImageTableViewCell.h"
#import "GymListViewController.h"
#import "I_Statistics.h"
#import "I_Gym.h"
#import "MemberListViewController.h"
#import "GroupRootViewController.h"
#import "ScheduleRootViewController.h"
#import "GymOverviewView.h"


@interface GymRootViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *statusBarViewHeightConstraint;

@property (weak, nonatomic) IBOutlet UIImageView *gymLogo;
@property (weak, nonatomic) IBOutlet UILabel *gymNameLabel;


@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) GymOverviewView *overviewView;
@property (nonatomic, strong) NSArray *dataSource;
@property (nonatomic, strong) M_GymOverview *gymOverview;
@end

@implementation GymRootViewController
#pragma mark Class Method
+ (instancetype)controller
{
    GymRootViewController *vc = [UIStoryboard controllerWithStoryBoardIdentify:NSStringFromClass([GymRootViewController class]) inStoryBoard:@"Main"];
    return vc;
}

#pragma mark Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
    _dataSource = @[@[@(GymRootVCCellType_GymGroup),@(GymRootVCCellType_GymMember)],@[@(GymRootVCCellType_PlanPreview),@(GymRootVCCellType_ClassAnalysis),@(GymRootVCCellType_ArrivalGymAnalysis),@(GymRootVCCellType_CoachAnalysis)]];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (RI.currentGym) {
        [self refreshGym];
        [self requestGymOverview];
    } else {
        WeakSelf(self);
        [I_Gym requestGymListOnSuccess:^(NSMutableArray *gymList) {
            RI.currentGym = gymList.firstObject;
            [weakself refreshGym];
            [weakself requestGymOverview];
        } failure:^(NSError *error) {
            [AV showRemind:@"获取健身房失败"];
        }];
    }
}

#pragma mark UI
- (void)configUI{
    _statusBarViewHeightConstraint.constant = [Toolkit getStatusBarHeight];
    _tableView.backgroundColor = TP.controllerBackColor;
    _tableView.insetTop = 10;
    [_tableView registerClass:[CommonImageTableViewCell class] forCellReuseIdentifier:NSStringFromClass([CommonImageTableViewCell class])];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;


    _gymLogo.layer.cornerRadius = 5;
    _gymLogo.layer.masksToBounds = YES;
    
    _overviewView = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([GymOverviewView class]) owner:nil options:nil].lastObject;
    _tableView.tableHeaderView = _overviewView;
}

- (void)refreshGym
{
    [_gymLogo setNetImageWithUrl:RI.currentGym.gymLogo];
    _gymNameLabel.text = RI.currentGym.gymName;
}

#pragma mark NET
- (void)requestGymOverview
{
    WeakSelf(self);
    [I_Statistics requestGymOverviewIsCheckAll:NO onSuccess:^(M_GymOverview *gymOverview) {
        [weakself.overviewView refreshUIWithGymOverview:gymOverview isCheckAll:NO];
        weakself.gymOverview = gymOverview;
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark Action

/**
 切换门店
 */
- (IBAction)switchGym {
    GymListViewController *listVC = [GymListViewController controllerWithType:GymListVCType_Switch];
    [self.navigationController pushViewController:listVC animated:YES];
}





#pragma mark UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *arr = _dataSource[section];
    return arr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommonImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CommonImageTableViewCell class])];
    GymRootVCCellType cellType = [_dataSource[indexPath.section][indexPath.row] integerValue];
    switch (cellType) {
        case GymRootVCCellType_GymGroup://团队
        {
            [cell refreshCellWithIconName:@"ic_50_tuandui" leftText:@"团队"];
        }
            break;
        case GymRootVCCellType_GymMember://会员
        {
            [cell refreshCellWithIconName:@"ic_50_huiyuan" leftText:@"会员"];
        }
            break;
        case GymRootVCCellType_PlanPreview://排课总览
        {
            [cell refreshCellWithIconName:@"ic_50_paikezonglan" leftText:@"排课总览"];
        }
            break;
        case GymRootVCCellType_ClassAnalysis://课时分析
        {
            [cell refreshCellWithIconName:@"ic_50_keshifenxi" leftText:@"课时分析"];
        }
            break;
        case GymRootVCCellType_ArrivalGymAnalysis://到店分析
        {
            [cell refreshCellWithIconName:@"ic_50_daodianfenxi" leftText:@"到店分析"];
        }
            break;
        case GymRootVCCellType_CoachAnalysis://教练分析
        {
            [cell refreshCellWithIconName:@"ic_50_jiaolianfenxi" leftText:@"教练分析"];
        }
            break;
            
        default:
            break;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    GymRootVCCellType cellType = [_dataSource[indexPath.section][indexPath.row] integerValue];
    switch (cellType) {
        case GymRootVCCellType_GymGroup://团队
        {
            GroupRootViewController *groupVC = [GroupRootViewController controller];
            [self.navigationController pushViewController:groupVC animated:YES];
        }
            break;
        case GymRootVCCellType_GymMember://会员
        {
            MemberListViewController *memberListVC = [MemberListViewController controllerWithMemberList:nil];
            [self.navigationController pushViewController:memberListVC animated:YES];
        }
            break;
        case GymRootVCCellType_PlanPreview://排课总览
        {
            ScheduleRootViewController *scheduleVC = [ScheduleRootViewController controller];
            [self.navigationController pushViewController:scheduleVC animated:YES];
        }
            break;
        case GymRootVCCellType_ClassAnalysis://课时分析
        {
            YDWebViewController *webVC = [YDWebViewController controllerWithUrl:Url_For_MemberClassAnalysis title:@"课时分析"];
            [self.navigationController pushViewController:webVC animated:YES];
        }
            break;
        case GymRootVCCellType_ArrivalGymAnalysis://到店分析
        {
            YDWebViewController *webVC = [YDWebViewController controllerWithUrl:Url_For_MemberArrivalStoreAnalysis title:@"到店分析"];
            [self.navigationController pushViewController:webVC animated:YES];
        }
            break;
        case GymRootVCCellType_CoachAnalysis://教练分析
        {
            YDWebViewController *webVC = [YDWebViewController controllerWithUrl:Url_For_CoachAnalysis title:@"教练分析"];
            [self.navigationController pushViewController:webVC animated:YES];
        }
            break;
            
        default:
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

@end
